package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.pojo.dto.KeFuOnLineLogSaveDTO;
import com.xbongbong.paas.pojo.vo.KeFuOnLineLogSaveVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.service.KeFuOnLineLogService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @version v1.0
 * @auther: youli.chen
 * @date 2021/3/31 10:12 上午
 * @since v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/keFuOnLine/log")
public class KeFuOnLineLogController {

    @Resource
    private KeFuOnLineLogService keFuOnLineLogService;

    /**
     * 保存
     * @param keFuOnLineLogSaveDTO
     * @param br
     * @return
     * 创建时间 2019/4/16 11:21 AM
     * 修改时间 2019/4/16 11:21 AM
     * @author youli.chen
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String receiptSave(@RequestBody @Valid KeFuOnLineLogSaveDTO keFuOnLineLogSaveDTO, BindingResult br) {
        XbbResponse<KeFuOnLineLogSaveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                KeFuOnLineLogSaveVO keFuOnLineLogSaveVO = keFuOnLineLogService.keFuOnLineLogSave(keFuOnLineLogSaveDTO);
                response = new XbbResponse<>(keFuOnLineLogSaveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
